{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing Prompt Functions with Deepseek Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-10-16 17:21:30.917\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-10-16 17:21:30,961\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-10-16 17:21:30,976\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    }
   ],
   "source": [
    "import byzerllm\n",
    "\n",
    "# Initialize the ByzerLLM with deepseek model\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！我是一个人工智能助手，专门设计来帮助回答问题、提供信息和进行对话。无论你有什么问题或需要帮助的地方，我都在这里为你服务。有什么我可以帮你的吗？\n"
     ]
    }
   ],
   "source": [
    "@byzerllm.prompt(llm=llm)\n",
    "def hello(q: str) -> str:\n",
    "    '''\n",
    "    你好, {{ q }}\n",
    "    '''\n",
    "\n",
    "response = hello(\"你是谁\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-10-17 22:36:52.283\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-10-17 22:36:52,321\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-10-17 22:36:52,338\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Person(full_name='张三', age=30, occupation='软件工程师', hobbies=['编程', '阅读', '旅行'])]\n",
      "```json\n",
      "{\n",
      "  \"full_name\": \"李四\",\n",
      "  \"age\": 30,\n",
      "  \"occupation\": \"软件工程师\",\n",
      "  \"hobbies\": [\"编程\", \"阅读\", \"旅行\"]\n",
      "}\n",
      "```\n",
      "\n",
      "  \"full_name\": \"李四\",\n",
      "  \"age\": 30,\n",
      "  \"occupation\": \"软件工程师\",\n",
      "  \"hobbies\": [\"编程\", \"阅读\", \"旅行\"]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel\n",
    "from typing import List\n",
    "import byzerllm\n",
    "\n",
    "# Initialize the ByzerLLM with deepseek model\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "class Person(BaseModel):\n",
    "    full_name: str\n",
    "    age: int\n",
    "    occupation: str\n",
    "    hobbies: List[str]\n",
    "\n",
    "class PersonF:\n",
    "    @byzerllm.prompt()\n",
    "    def get_person_info(self,name: str, age: int) -> str:\n",
    "        \"\"\"\n",
    "        请提供一个关于以下人物的简短描述：\n",
    "        \n",
    "        姓名: {{ name }}\n",
    "        年龄: {{ age }}\n",
    "        \n",
    "        描述应包括以下字段的信息：\n",
    "        - full_name: 完整姓名\n",
    "        - age: 年龄\n",
    "        - occupation: 可能的职业\n",
    "        - hobbies: 可能的兴趣爱好（列表形式）\n",
    "        \n",
    "        请以 JSON 格式输出结果。\n",
    "        \"\"\"\n",
    "p = PersonF()\n",
    "k = p.get_person_info.with_llm(llm)\n",
    "v = k.with_return_prefix(\"[{\").with_return_type(Person).run(name=\"张三\", age=30)\n",
    "print(v)\n",
    "\n",
    "p2 = PersonF()\n",
    "k2 = p2.get_person_info.with_llm(llm)\n",
    "v2 = k2.run(name=\"李四\", age=30)\n",
    "print(v2)\n",
    "\n",
    "p3 = PersonF()\n",
    "k3 = p3.get_person_info.with_llm(llm)\n",
    "json_prefix = \"{\"\n",
    "v3 = k3.with_return_prefix(json_prefix).with_stop_suffix_list([\"}\"]).run(name=\"李四\", age=30)\n",
    "print(v3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-10-18 09:56:05.085\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-10-18 09:56:05,122\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-10-18 09:56:05,137\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<byzerllm._DescriptorPrompt object at 0x13e2d5e70>\n",
      "[Person(full_name='张三', age=30, occupation='软件工程师', hobbies=['编程', '阅读', '旅行'])]\n",
      "<byzerllm._DescriptorPrompt object at 0x13e2d5e70>\n",
      "[Person(full_name='李四', age=30, occupation='软件工程师', hobbies=['编程', '阅读', '旅行'])]\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel\n",
    "from typing import List\n",
    "import byzerllm\n",
    "\n",
    "# Initialize the ByzerLLM with deepseek model\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "class Person(BaseModel):\n",
    "    full_name: str\n",
    "    age: int\n",
    "    occupation: str\n",
    "    hobbies: List[str]\n",
    "\n",
    "\n",
    "@byzerllm.prompt()\n",
    "def get_person_info(name: str, age: int) -> str:\n",
    "    \"\"\"\n",
    "    请提供一个关于以下人物的简短描述：\n",
    "    \n",
    "    姓名: {{ name }}\n",
    "    年龄: {{ age }}\n",
    "    \n",
    "    描述应包括以下字段的信息：\n",
    "    - full_name: 完整姓名\n",
    "    - age: 年龄\n",
    "    - occupation: 可能的职业\n",
    "    - hobbies: 可能的兴趣爱好（列表形式）\n",
    "    \n",
    "    请以 JSON 格式输出结果。\n",
    "    \"\"\"\n",
    "\n",
    "k = get_person_info.with_llm(llm)\n",
    "print(k)\n",
    "v = k.with_return_prefix(\"[{\").with_return_type(Person).run(name=\"张三\", age=30)\n",
    "print(v)\n",
    "\n",
    "\n",
    "k2 = get_person_info.reset().with_llm(llm)\n",
    "print(k2)\n",
    "v2 = k2.run(name=\"李四\", age=30)\n",
    "print(v2)\n",
    "\n",
    "print(k == k2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-10-17 22:42:59.032\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-10-17 22:42:59,085\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-10-17 22:42:59,086\tINFO worker.py:1582 -- Calling ray.init() again after it has already been called.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "，然后给出答案。\n",
      "\n",
      "首先，我们需要知道阶乘的定义。对于一个正整数 \\( n \\)，其阶乘 \\( n! \\) 定义为从 1 到 \\( n \\) 的所有正整数的乘积。\n",
      "\n",
      "因此，7的阶乘 \\( 7! \\) 可以表示为：\n",
      "\\[ 7! = 7 \\times 6 \\times 5 \\times 4 \\times 3 \\times 2 \\times 1 \\]\n",
      "\n",
      "接下来，我们逐步计算这个乘积：\n",
      "\n",
      "1. 计算 \\( 7 \\times 6 \\):\n",
      "\\[ 7 \\times 6 = 42 \\]\n",
      "\n",
      "2. 计算 \\( 42 \\times 5 \\):\n",
      "\\[ 42 \\times 5 = 210 \\]\n",
      "\n",
      "3. 计算 \\( 210 \\times 4 \\):\n",
      "\\[ 210 \\times 4 = 840 \\]\n",
      "\n",
      "4. 计算 \\( 840 \\times 3 \\):\n",
      "\\[ 840 \\times 3 = 2520 \\]\n",
      "\n",
      "5. 计算 \\( 2520 \\times 2 \\):\n",
      "\\[ 2520 \\times 2 = 5040 \\]\n",
      "\n",
      "6. 计算 \\( 5040 \\times 1 \\):\n",
      "\\[ 5040 \\times 1 = 5040 \\]\n",
      "\n",
      "因此，7的阶乘等于：\n",
      "\\[ 7! = 5040 \\]\n",
      "\n",
      "最终答案是：\n",
      "\\[ \\boxed{5040\n"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel\n",
    "from typing import List\n",
    "import byzerllm\n",
    "\n",
    "# Initialize the ByzerLLM with deepseek model\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "class PersonF:\n",
    "    @byzerllm.prompt()\n",
    "    def get_person_info(self) -> str:\n",
    "        \"\"\"\n",
    "        7的阶乘 等于多少\n",
    "        \"\"\"\n",
    "p3 = PersonF()\n",
    "k3 = p3.get_person_info.with_llm(llm)\n",
    "json_prefix = \"我们不直接做计算，而是给出先给出一个推倒逻辑\"\n",
    "v3 = k3.with_return_prefix(json_prefix).run()\n",
    "print(v3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "byzerllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
